
public class HoleSizeFeature implements Feature {

	@Override
	public double computeScore(Stateview s) {
		int[][] field = s.getField();
		int[]top = s.getTop();
		
		int holeSize = 0;
		boolean calculated[][] = new boolean[Stateview.ROWS][Stateview.COLS];
		for(int i = 0; i < Stateview.COLS; i++)
			for(int j = 0; j < Stateview.ROWS; j++) 
				calculated[j][i] = false;

		for(int col = 0; col < Stateview.COLS; col++)
		{
			for(int row = 0; row < Stateview.ROWS; row++)
			{
				if(field[row][col] == 0 && !calculated[row][col])
				{
					if(holeSize == 0) holeSize = 1;
					holeSize *= computeHoleSize(field, calculated, col, row);
				}
			}
		}
		return -holeSize*10;
	}
	
	//End Heuristics
	// avoid recursive if possible
	// not really efficient
	private int computeHoleSize(int[][] field, boolean [][] calculated, int colNo, int rowNo)
	{
		if(calculated[rowNo][colNo]) 
		{
			return 0;
		}
		int holeSize = 1;
		calculated[rowNo][colNo] = true;
		for(int i = colNo; i < colNo+1; i++)
		{
			if(i < Stateview.COLS)
			{
				for(int j = rowNo; j < rowNo + 1; j++)
				{
					if(j < Stateview.ROWS && field[j][i] == 0)
					{
						holeSize += computeHoleSize(field, calculated, i, j);
					}
				}
			}
		}
		return holeSize;                                  
	}
	
}
